package cn.tedu.ems.studyfile.service.impl;


import cn.tedu.ems.base.exception.ServiceException;
import cn.tedu.ems.base.response.StatusCode;
import cn.tedu.ems.studyfile.mapper.ApplicationMapper;
import cn.tedu.ems.studyfile.pojo.dto.*;
import cn.tedu.ems.studyfile.pojo.vo.StudyFileVO;
import cn.tedu.ems.studyfile.pojo.entity.StudyRelease;
import cn.tedu.ems.studyfile.service.ApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;


@Slf4j
@Service
@Transactional
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    private ApplicationMapper applicationMapper;

    //    查询所有分类
    @Override
    public List<CategoryListDTO> selectfile() {
        List<CategoryListDTO> list = applicationMapper.selectfile();
        log.debug("list:{}", list);
        return list;
    }


    //    按 状态,标题,分类 搜索
    @Override
    public List<StudyFileVO> selectListsTitleStateArticleVideoCategoryId(SearchFor searchFor) {
        log.debug("入参searchFor:{}", searchFor);
        List<StudyFileVO> list = applicationMapper.selectListsTitleStateArticleVideoCategoryId(searchFor);
        return list;
    }

    // 发布文章
    @Override
    public boolean insertRelease(StudyReleaseDTO studyReleaseDTO) {
        log.debug("studyReleaseDTO:{}", studyReleaseDTO);
        if (StringUtils.isBlank(studyReleaseDTO.getContent()) || StringUtils.isBlank(studyReleaseDTO.getTitle()) || studyReleaseDTO.getArticleVideoCategoryId() == null) {
            //检查 studyReleaseDTO 对象中的 content、title 和 articleVideoCategoryId 字段是否为空或未设置
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        } else {
            StudyRelease studyRelease = new StudyRelease();
//            studyRelease.setVideoAddress("www.baidu.com");
            studyRelease.setPublisher("admin");
            studyRelease.setCreateTime(new Date());
            studyRelease.setUpdateTime(new Date());
            studyRelease.setContent(studyReleaseDTO.getContent());
            studyRelease.setTitle(studyReleaseDTO.getTitle());
            studyRelease.setArticleVideoCategoryId(studyReleaseDTO.getArticleVideoCategoryId());
            applicationMapper.insertRelease(studyRelease);
            log.debug("发布成功");
            return true;
        }
    }

//    修改状态
    @Override
    public boolean updateState(StateDTO stateDTO) {
        log.debug("入参stateDTO:{}", stateDTO);
        String state =  applicationMapper.selectState(stateDTO.getId());
        log.debug("state:{}",state);
        if(state.equals("1")){
            stateDTO.setState("-1");
           applicationMapper.updateState(stateDTO);
        } else if (state.equals("-1")) {
            stateDTO.setState("1");
            applicationMapper.updateState(stateDTO);
        }
        return true;
    }

//    更新文章
    @Override
    public boolean updateArticle(StudyUpdateArticle studyUpdateArticle) {
        log.debug("studyUpdateArticle:{}",studyUpdateArticle);
        studyUpdateArticle.setUpdateTime(new Date());
        boolean b = applicationMapper.updateArticle(studyUpdateArticle);
        return b;
    }

    @Override
    public boolean deleteArticle(StudyDeleteDTO studyDeleteDTO) {
        log.debug("studyDeleteDTO:{}",studyDeleteDTO);
        studyDeleteDTO.setDeleteTime(new Date());
        boolean b = applicationMapper.updateDeleteTime(studyDeleteDTO);
        return b;
    }

    /**
     * 根据ID查询详情
     *
     * @param id
     * @return
     */
    @Override
    public StudyFileVO selectById(Integer id) {
        StudyFileVO studyFileVO = applicationMapper.selectById(id);
        return studyFileVO;
    }
}